﻿/*
 * Created by SharpDevelop.
 * User: André Casaca
 * Date: 17-06-2011
 * Time: 15:18
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Data;
using Mono.Data.SqliteClient;
using System.Text;
using System.Linq;


namespace LP_C__5847_2010_2011
{
	/// <summary>
    /// The Main Class
    /// </summary>
	class Program
	{
		/// <summary>
        /// The constructor runs all the program doing all the questions and
        /// commends to run the program correctly
        /// </summary>
		public static void Main()
		{
			Console.Title = "Rebides Statistics";
			Console.SetWindowSize(120, 60);
			
			string s = "";
      		string strYear = "";
			do{
      			Console.ForegroundColor = ConsoleColor.DarkGreen;	
      			Console.WriteLine("Estatistics: ");
      			Console.ForegroundColor = ConsoleColor.White;
				Console.WriteLine("\n 1=total number of teachers in the higher education system per year" +
					              "\n 2=total number of teachers per establishment and per year " +
					              "\n 3=total number of teachers per degree and per year" +
					              "\n 4=total number of teachers per degree, per establishment and per year" +
					              "\n 5=The number of holders of a doctorate degree per establishment and per year" +
					              "\n 6=The set of holders of a doctorate degree per establishment and per year" +
					              "\n 7=The set of teachers that changed from one establishment to another one per year" +
					              "\n 8=list of establishments per year" +
					              "\n 9=list of holders of a degree per year" +
					              "\n 10=personnel leaving the institution per year" +
					              "\n 11=new personnel in the institution per year" +
					              "\n 12=number of teachers migrating from one establishment to another" +
					              "\n 13=number of teachers promoted to the next category each year per establishment" +
					              "\n" +
					              "\n 14=save all statistics on text" +
					              "\n\n exit=Exit the program");
      				
      				
      				try{
						Console.Write("\n Please enter your selection: ");
						Console.ForegroundColor = ConsoleColor.Blue;						
      					s = Console.ReadLine(); 
      					int n = int.Parse(s);
      					Console.ForegroundColor = ConsoleColor.White;
      					
      					int year = 0;
      					int sortType = 0;
      					if(n > 0 && n <= 14){
      						Console.Write("\n Please enter the year: "); 
      						Console.ForegroundColor = ConsoleColor.Blue;
      						strYear = Console.ReadLine(); 
      						year = int.Parse(strYear) - 2000;
      						Console.ForegroundColor = ConsoleColor.White;
      						
      						if(n < 6 && n != 1 || n == 13 || n == 14){
      							Console.Write("\n Sorting Type: [ByName/Key = 1; ByNumbers/value = 2; DoNotSort >= 3] : "); 
      							Console.ForegroundColor = ConsoleColor.Blue;
      							string strType = Console.ReadLine(); 
      							sortType = int.Parse(strType);
      							Console.ForegroundColor = ConsoleColor.White;
      						}
      					}
      					
      					if(year >= 0 && year <= 9){
      						StartCases(n, year, sortType);
      					}else{
      						Console.ForegroundColor = ConsoleColor.Red;
      						Console.Write("\nInvalid year. 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 and 2009\n\n"); 
      						Console.ForegroundColor = ConsoleColor.White;
      					}
      				}catch(Exception e){
      					if(!s.Equals("exit") && !strYear.Equals("exit")){
      						Console.ForegroundColor = ConsoleColor.Red;
      						Console.Write("\n Incorrect Format or year! \n\n" + e);
      						Console.ForegroundColor = ConsoleColor.White;
      					}
      				}
			}while(!s.Equals("exit") && !strYear.Equals("exit"));
      		
      		Console.Write("\n Exit the program, waiting for any key to close...");
      		
      		Console.ReadKey();
		}
		
		/// <summary>
        /// Do all the Switch cases and run the statistic chosed
        /// </summary>
        /// <param name="n">the selection chosed by the user</param>
        /// <param name="year">the year chosed by the user</param>
        /// <param name="sortType">the sort chosed by the user</param>
		public static void StartCases(int n, int year, int sortType){
			Console.ForegroundColor = ConsoleColor.Blue;
			switch(n)       
      				{         
         				case 1: 
							Console.Write("\n Year : {0}  ->  {1} teachers", year + 2000, Statistics.GetNumTeachers(year));				
           					break;                  
         				case 2:            
           					Dictionary<string, int> establishmentDict = Statistics.NumberTeachersEstablishmentYear(year, "estabelecimentos");
           					SortType(establishmentDict, sortType);
           					break;          
         				case 3:            
							Dictionary<string, int> degreeDict = Statistics.NumberTeachersEstablishmentYear(year, "graus");	
							SortType(degreeDict, sortType);							
           					break;   
						case 4:
							Dictionary<string, int> establishmentdegreeDict = Statistics.NumberTeachersEstablishmentYear(year, "graus,estabelecimentos");  
							SortType(establishmentdegreeDict, sortType);							
           					break;                  
         				case 5:            
            				Dictionary<string, int> teacherDoctDict = Statistics.NumTeachersDoctorYear(year);
            				SortType(teacherDoctDict, sortType);
							break;            				
         				case 6: 
							//its alredy sorted							
           					Statistics.ListTeachersDoctorYear(year);
							break;          					
						case 7:  
							//its alredy sorted
							Statistics.ListMigratersTeachers(year);					
           					break;                  
         				case 8:            
            				List<string> listEstablishments = Statistics.PrintListEstablishments(year);       				
            				listEstablishments.Sort();
							foreach (string k in listEstablishments){
								Console.WriteLine(k);
							}
            				break;
         				case 9:            
           					List<Person> listTeachers = Statistics.GetListTeachers(year);
           					List<string> listNameTeachers = new List<string>();
           					foreach (Person k in listTeachers){
           						if(!listNameTeachers.Contains(k.GetName)){
           							listNameTeachers.Add(k.GetName);
           						}				
							}
           					listNameTeachers.Sort();
							foreach (string k in listNameTeachers){
								Console.WriteLine(k);
							}
           					break;
						case 10:
							//its alredy sorted           					
							Statistics.PrintLeaversNewersList(year, 10);           					
           					break;                  
         				case 11:  
							//its alredy sorted           					
            				Statistics.PrintLeaversNewersList(year, 11);
							break;            				
         				case 12:  
							//dont need sort							
							Statistics.NumberMigratersTeachers(year);
           					break;
						case 13:  
           					//dont need sort
							Dictionary<string, int> teacherPromList = Statistics.NumberPromotedTeachers(year);	
							SortType(teacherPromList, sortType);							
           					break;                  
         				case 14:            
           					new Write(year, sortType);
							break;            					
         				default:          				
            				Console.WriteLine("\nInvalid selection...");            				
            				break;      
       				}
			Console.Write("\n\n");
			Console.ForegroundColor = ConsoleColor.White;
		}
        
        /// <summary>
        /// do the sort that user wants
        /// </summary>
        /// <param name="dict">dictinary to sort</param>
        /// <param name="sortType">the sort chosed by the user</param>
		public static Dictionary<string, int> SortType(Dictionary<string, int> dict , int sortType){
			if(sortType == 1){
				dict = SortDictionaryByValue(dict);
			}else if(sortType == 2){
				dict = SortDictionaryByKey(dict);
			}else{
				foreach(KeyValuePair<string, int> entry in dict)
            	{
				Console.WriteLine("{0}: {1}", entry.Key, entry.Value);
				}
				Console.WriteLine("\n\nNot Sorted...");
			}
			return dict;
		}
		
        /// <summary>
        /// Sort a dictionary by value
        /// </summary>
        /// <param name="dict">dictinary to sort</param>
		public static Dictionary<string, int> SortDictionaryByValue(Dictionary<string, int> dict){
			List<KeyValuePair<string, int>> sorted = (from entry in dict orderby entry.Value select entry).ToList();
			Dictionary<string, int> sortedDict = new Dictionary<string, int>();
			 foreach (KeyValuePair<string, int> entry in sorted)
            {
				Console.WriteLine("{0}: {1}", entry.Key, entry.Value);
				sortedDict.Add(entry.Key, entry.Value);
			}
			 return sortedDict;
		}
		
        /// <summary>
        /// Sort a dictionary by Key
        /// </summary>
        /// <param name="dict">dictinary to sort</param>
		public static Dictionary<string, int> SortDictionaryByKey(Dictionary<string, int> dict){
			List<KeyValuePair<string, int>> sorted = (from entry in dict orderby entry.Key select entry).ToList();
			Dictionary<string, int> sortedDict = new Dictionary<string, int>();
			 foreach (KeyValuePair<string, int> entry in sorted)
            {
				Console.WriteLine("{0}: {1}", entry.Key, entry.Value);
				sortedDict.Add(entry.Key, entry.Value);
			}
			 return sortedDict;
		}
		
	}
}
	
	
